import logging

import requests
class Test1():
    uid=""
    sid=""
    def setup_method(self):
        print("初始化环境")
    def teardown_method(self):
        print("恢复环境")
    #登录
    def test_dl(self):
        try:
            url="http://10.59.9.18/small/user/v1/login"
            body={"phone":"13811033965","pwd":"123456Aa"}
            head={"Content-Type":"application/json"}
            r=requests.post(url=url,json=body,headers=head)
            print("登录成功",r.json())
            logging.log(r.json())
            Test1.uid=r.json()['result']["userId"]
            Test1.sid=r.json()['result']['sessionId']
        except Exception as e:
            logging.info(e)
            assert r.status_code==200
            assert '登录成功' in r.text
            assert r.json()['message']=='登录成功'
    #查看商品详情
    def test_chak(self):
        try:
            url = "http://10.59.9.18/small/commodity/v1/findCommodityDetailsById?commodityId=3"
            head = {"Content-Type": "application/json"}
            r = requests.get(url=url,headers=head)
            print("查看成功", r.json())
            logging.log(r.json())
        except Exception as e:
            logging.info(e)
            assert r.status_code == 200
            assert '美妆护肤' in r.text
            assert r.json()['result']["categoryName"] == '美妆护肤'
#查看数量
    def test_sl(self):
        try:
            url = "http://10.59.9.18/small/commodity/v1/getInventory"
            body={"commodityId":10}
            head = {"Content-Type": "application/json"}
            r = requests.get(url=url,params=body,headers=head)
            print('查询成功', r.json())
            logging.log(r.json())
        except Exception as e:
            logging.info(e)
            assert r.status_code == 200
            assert '查询成功' in r.text
            assert r.json()['message'] == '查询成功'
#创建订单
    def test_cj(self):
        try:
            url = "http://10.59.9.18/small/order/verify/v1/createOrder"
            body ={"orderInfo":{"commodityId":3,"amount":1},"totalPrice":{"commodityId":55,"amount":1},"addressId":1}
            head = {"userId":str(Test1.uid),"sessionId":Test1.sid}
            r = requests.post(url=url, data=body, headers=head)
            print('创建成功', r.json())
            logging.log(r.json())
        except Exception as e:
            logging.info(e)
            # assert r.status_code == 200
            # assert '查询成功' in r.text
            # assert r.json()['message'] == '查询成功'
